Utility -- hexString, dumpMemory, DXL int fun facts.

Sooner or later many of us want to show integers in hex format. Since DXL does not come with one out of the box, I wrote one. I noticed some peculiarities in certain situations. It all stems from the fact you can't use a DXL int to transport arbitrary DWORD (full 32-bit) content. DXL will only allow +/- 2147438647 decimal. If you are trying to accurately show memory content in a certain range, this can lead to some distortions. If you attempt to load a number larger than 2147438647, DXL will auto-limit it to the 2147438647 ceiling. Likewise for negative. DXL is the first language I've encountered with this limitation (feature?).

In most cases, it does not present a problem but if you are trying to look at content of memory locations, I want to see what is really there (not a view limited by DXL int range).

I believe I found a way to accurately see memory content in hex without the DXL int limitations.

I show some sample output followed by the .INC and .DXL files.

If anyone sees any bugs let me know.
 

OUTPUT:

Variable someInt was set to -2147483648 and now it is 0x80000001 (-2147483647) decimal
Variable someInt was set to -2147483647 and now it is 0x80000001 (-2147483647) decimal
Variable someInt was set to -2147483646 and now it is 0x80000002 (-2147483646) decimal
Variable someInt was set to 0x0FFFFFFF and now it is 0x0FFFFFFF (268435455) decimal
Variable someInt was set to 0x1FFFFFFF and now it is 0x1FFFFFFF (536870911) decimal
Variable someInt was set to 0x3FFFFFFF and now it is 0x3FFFFFFF (1073741823) decimal
Variable someInt was set to 0x7FFFFFFF and now it is 0x7FFFFFFF (2147483647) decimal
Variable someInt was set to 0xFFFFFFFF and now it is 0x7FFFFFFF (2147483647) decimal
If you want to have control over the most significant byte,you can do this (3 is the offset byte):
HexMem_loadByteContentIntoIntReference(0xFF, someInt, 3);

Variable someInt upper byte changed to 0xFF and now it is 0xFFFFFFFF (-1) decimal

Here is the assigned variable:
0x0919E5B0 = 0xFFFFFFFF ASCII: "ÿÿÿÿ"
Here is the unassigned variable:
0x0919E5B4 = 0x87654321 ASCII: "‡eC!"

CODE SNIPPET 1 OF 2 (.INC) FILE:

//------------------------------------------------------------------------------
// BEGIN HexMem Utility
// * Converts int variables to their hex string representation.
// * Also has an explore memory utility to show ranges of memory in hex.
// * Has several data-move and type-operation utilitites that may be of
//   general use.
//
// * In general if you directly use DXL integers in moving data, DXL will
//   reduce the range to 31 bits and possibly alter the true content.  Try
//   setting an int to 0xFFFFFFFF and see what it is later.  This utility uses
//   bytes to get around DXL altering the data.
//------------------------------------------------------------------------------
 
// *****************************************************************************
// ***** PUBLIC FUNCTIONS ******************************************************
// *****************************************************************************
//------------------------------------------------------------------------------
// Tries to return the char itself.  If unprintable, returns copyright symbol
//------------------------------------------------------------------------------
// char HexMem_safeCharOf(int my32BitInt);
//------------------------------------------------------------------------------
char HexMem_safeCharOf(int);
 
//------------------------------------------------------------------------------
// Tries to return the char itself.  If unprintable, returns copyright symbol
//------------------------------------------------------------------------------
// char HexMem_safeCharOf(char candidate);
//------------------------------------------------------------------------------
char HexMem_safeCharOf(char);
 
//------------------------------------------------------------------------------
// The below returns the address of a variable as "pointer to int"
//------------------------------------------------------------------------------
// int* HexMem_intStarAddressOf(_m& v);
//------------------------------------------------------------------------------
int* HexMem_intStarAddressOf(_m&);
 
//------------------------------------------------------------------------------
// The below returns the address of a simple variable as an integer
//------------------------------------------------------------------------------
// int HexMem_addressOfVar(_m& anyVar);
//------------------------------------------------------------------------------
int HexMem_addressOfVar(_m&);
 
//------------------------------------------------------------------------------
// void HexMem_appendHexByteToBuffer(Buffer b,
//                                   int byte);
//------------------------------------------------------------------------------
void HexMem_appendHexByteToBuffer(Buffer, int);
 
//------------------------------------------------------------------------------
// void HexMem_appendSafeCharToBuffer(Buffer b,
//                                    int byte);
//------------------------------------------------------------------------------
void HexMem_appendSafeCharToBuffer(Buffer, int);
 
//------------------------------------------------------------------------------
// int HexMem_readByteContentFromIntReference(int& intRef,
//                                            int offset);
//------------------------------------------------------------------------------
int HexMem_readByteContentFromIntReference(int&, int);
 
//------------------------------------------------------------------------------
// void HexMem_copyContentInP1ToContentInP2(int& p1, int& p2);
//------------------------------------------------------------------------------
void HexMem_copyContentInP1ToContentInP2(int&, int&);
 
//------------------------------------------------------------------------------
// void HexMem_loadByteContentIntoIntReference(int newByteContent,
//                                             int& intRef,
//                                             int offsetInDword);
//------------------------------------------------------------------------------
void HexMem_loadByteContentIntoIntReference(int, int&, int);
 
//------------------------------------------------------------------------------
// The methodology may seem convoluted but if you don't do it this way you may
// get an altered most significant byte due to DXL max int restrictions.
//------------------------------------------------------------------------------
// void HexMem_appendHexStringOfMemContent(Buffer bHexIntegers,
//                                         Buffer bAsciiCharacters,
//                                         int memAddress,
//                                         int size_8_16_or_32,
//                                         bool want0xPrefix);
//------------------------------------------------------------------------------
void HexMem_appendHexStringOfMemContent(Buffer, Buffer, int, int, bool);
 
//------------------------------------------------------------------------------
// Buffers must be pre-initialized
//------------------------------------------------------------------------------
// void HexMem_appendHexStringOfInt(Buffer bHexInteger,
//                                  Buffer bAsciiCharacters,
//                                  int myInt,
//                                  bool want0xPrefix);
//------------------------------------------------------------------------------
void HexMem_appendHexStringOfInt(Buffer, Buffer, int, bool);
 
//------------------------------------------------------------------------------
// Buffers must be pre-initialized
//------------------------------------------------------------------------------
// void HexMem_appendHexStringOfInt(Buffer bHexInteger,
//                                  Buffer bAsciiCharacters,
//                                  int myInt,
//                                  bool want0xPrefix);
//------------------------------------------------------------------------------
void HexMem_appendHexStringOfInt(Buffer, Buffer, int, bool);
 
//------------------------------------------------------------------------------
// Buffers must be pre-initialized
//------------------------------------------------------------------------------
// void HexMem_appendHexStringOfInt(Buffer bHexInteger,
//                                  int myInt,
//                                  bool want0xPrefix);
//------------------------------------------------------------------------------
void HexMem_appendHexStringOfInt(Buffer, int, bool);
 
//------------------------------------------------------------------------------
// string HexMem_hexStringOf(int myInt, bool want0xPrefix);
//------------------------------------------------------------------------------
string HexMem_hexStringOf(int, bool);
 
//------------------------------------------------------------------------------
// Returns an int in its hex representation with 0x prefix
//------------------------------------------------------------------------------
// string HexMem_hexStringOf(int myInt);
//------------------------------------------------------------------------------
string HexMem_hexStringOf(int);
 
//------------------------------------------------------------------------------
// With a SkipString object, loads addresses into the key and content into the
// data.  You specify the start address and lengthInDwords.
//
// User options are:
// 1) use 0x prefix
// 2) add characters onto the end of the data (if printable)
//------------------------------------------------------------------------------
// void HexMem_exploreAddressesIntoSkip(Skip skStrStr,
//                                      int start,
//                                      int lengthInDwords,
//                                      bool use0xPrefix,
//                                      bool addAsciiChars);
//------------------------------------------------------------------------------
void HexMem_exploreAddressesIntoSkip(Skip, int, int, bool, bool);
 
//------------------------------------------------------------------------------
// With a pre-initialized SkipString object, loads addresses into the key and
// content into the data.  You specify the start address and lengthInDwords.
//
// Fixed Options are:
// 1) Will use 0x prefix
// 2) Will add characters onto the end of the data (if printable)
//------------------------------------------------------------------------------
// void HexMem_exploreAddressesIntoSkip(Skip skStrStr,
//                                      int start,
//                                      int lengthInDwords);
//------------------------------------------------------------------------------
void HexMem_exploreAddressesIntoSkip(Skip, int, int);
 
//------------------------------------------------------------------------------
// Sends 0xHexAddress = 0xHexContent CCCC to the  void outFunction(string) you
// specify as parameter 3.
//
// You specify the start address and lengthInDwords.  Fixed Options are:
// 1) Will use 0x prefix
// 2) Will add characters (C) onto the end of the data (if printable)
//------------------------------------------------------------------------------
// void HexMem_exploreAddressesToFunction(int start,
//                                        int lengthInDwords,
//                                        void outFunction(string));
//------------------------------------------------------------------------------
void HexMem_exploreAddressesToFunction(int, int, void (string));
 
//------------------------------------------------------------------------------
// Prints 0xHexAddress = 0xHexContent CCCC
// You specify the start address and lengthInDwords.  Fixed Options are:
// 1) Will use 0x prefix
// 2) Will add characters (C) onto the end of the data (if printable)
//------------------------------------------------------------------------------
// void HexMem_exploreAddressesToPrint(int start,
//                                     int lengthInDwords);
//------------------------------------------------------------------------------
void HexMem_exploreAddressesToPrint(int, int);
 
//------------------------------------------------------------------------------
// END HexMem Utility
//------------------------------------------------------------------------------

 

 

CODE SNIPPET (2 of 2) DXL Source:

 

 

 

//------------------------------------------------------------------------------
// BEGIN HexMem Utility
//------------------------------------------------------------------------------
// * Converts int variables to their hex string representation.
// * Also has an explore memory utility to show ranges of memory in hex.
// * Has several data-move and type-operation utilitites that may be of
//   general use.
//
// * In general if you directly use DXL integers in moving data, DXL will
//   reduce the range to 31 bits and possibly alter the true content.  Try
//   setting an int to 0xFFFFFFFF and see what it is later.  This utility uses
//   bytes to get around DXL altering the data.
//------------------------------------------------------------------------------
 
//------------------------------------------------------------------------------
// Tries to return the char itself.  If unprintable, returns copyright symbol
//------------------------------------------------------------------------------
char HexMem_safeCharOf(int my32BitInt) {
    char retVal = charOf(169);
    char candidate;
    if ( (my32BitInt > -1) && (my32BitInt < 256) ) {
        candidate = charOf(my32BitInt);
        if (isprint(candidate)) {
            retVal= candidate
        }
    }
    return retVal;
}
 
//------------------------------------------------------------------------------
// Tries to return the char itself.  If unprintable, returns copyright symbol
//------------------------------------------------------------------------------
char HexMem_safeCharOf(char candidate) {
    return (HexMem_safeCharOf(int addr_ candidate));
}
 
//------------------------------------------------------------------------------
// The below returns the address of a variable as "pointer to int"
//------------------------------------------------------------------------------
int* HexMem_intStarAddressOf(_m& v) {
    int* val = &(addr_ (int (addr_ v)));
    return (val);
}
 
//------------------------------------------------------------------------------
// The below returns the address of a simple variable as an integer
//------------------------------------------------------------------------------
int HexMem_addressOfVar(_m& anyVar) {
    int temp = (int addr_ HexMem_intStarAddressOf(anyVar));
    return temp;
}
 
void HexMem_appendHexByteToBuffer(Buffer b,
                                  int byte) {
    const string hexMap[] = {"0","1","2","3","4","5","6","7", //-
                             "8","9","A","B","C","D","E","F"};
    int highNibble = (byte & 0x000000F0) / 16;
    int lowNibble  = (byte & 0x0000000F);
    b += hexMap[highNibble];
    b += hexMap[lowNibble];
}
 
void HexMem_appendSafeCharToBuffer(Buffer b,
                                   int byte) {
    b += HexMem_safeCharOf(byte);
}
 
int HexMem_readByteContentFromIntReference(int& intRef,
                                           int offset) {
    int* intAddr = HexMem_intStarAddressOf(intRef);
    intAddr += offset;
    int myByte = 0;
    myByte = (*intAddr) & 0x000000FF;
    return myByte;
}
 
 
void HexMem_copyContentInP1ToContentInP2(int& p1, int& p2) {
    p2 = p1;
}
 
void HexMem_loadByteContentIntoIntReference(int newByteContent,
                                            int& intRef,
                                            int offsetInDword) {
    // BEGIN -- private function
    void HexMem_loadByteContentIntoIntReference_(int byteContent,
                                                 int& intRef,
                                                 int offset) {
        int* intAddr = HexMem_intStarAddressOf(intRef);
        intAddr += offset;
        (*intAddr) = (*intAddr) & 0x7FFFFF00;
        (*intAddr) = (*intAddr) | byteContent;
    }
    // END -- private function
 
    if ((offsetInDword < 0) || (offsetInDword > 3)) {
        ack "HexMem_loadByteContentIntoIntReference(): " //-
            "offsetInDword range is 0..3, bad value: " (offsetInDword) "";
        halt;
    }
    //--------------------------------------------------------------------------
    // Make a clean operating zone.  Operations can alter content of the next
    // memory location.  Try making iNext 0x7FFFFFFF and you'll see what I mean.
    // If we do the deed here where the next memory location is throwaway, all
    // is well and it works as advertised.
    //--------------------------------------------------------------------------
    int iOperation = 0;
    int iNext      = 0;
 
    //--------------------------------------------------------------------------
    // Step 1 -- Get a copy of the patient's content (intRef)
    //--------------------------------------------------------------------------
    HexMem_copyContentInP1ToContentInP2(intRef, iOperation);
 
    const int BYTES_IN_A_DWORD = 4;
    int bytes[BYTES_IN_A_DWORD];
    int i;
    //--------------------------------------------------------------------------
    // Step 2 -- Record the original content as bytes;
    //--------------------------------------------------------------------------
    for(i = 0; i < BYTES_IN_A_DWORD; i++) {
        bytes[i] = HexMem_readByteContentFromIntReference(iOperation, i);
    }
    //--------------------------------------------------------------------------
    // Step 3 -- Copy the orginal content back as bytes but substitute
    //           the newByteContent requested by the caller;
    //--------------------------------------------------------------------------
    for(i = 0; i < BYTES_IN_A_DWORD; i++) {
      if (i == offsetInDword) {
          // Private function that may alter next mem location
          HexMem_loadByteContentIntoIntReference_(newByteContent, iOperation, i);
      } else {
          // Private function that may alter next mem location
          HexMem_loadByteContentIntoIntReference_(bytes[i], iOperation, i);
      }
    }
 
    //--------------------------------------------------------------------------
    // Replace the original intRef content with iOperation content
    //--------------------------------------------------------------------------
    HexMem_copyContentInP1ToContentInP2(iOperation, intRef);
}
 
 
//------------------------------------------------------------------------------
// The methodology may seem convoluted but if you don't do it this way you may
// get an altered most significant byte due to DXL max int restrictions.
//------------------------------------------------------------------------------
void HexMem_appendHexStringOfMemContent(Buffer bHexIntegers,
                                        Buffer bAsciiCharacters,
                                        int memAddress,
                                        int size_8_16_or_32,
                                        bool want0xPrefix) {
    if (null(bHexIntegers)) {
        ack "HexMem_appendHexStringOfMemContent bad buffer passed in";
        halt;
    }
 
    bool wantCharsToo = true;
    if (null(bAsciiCharacters)) {
        wantCharsToo = false;
    }
 
    int byte;
    if (want0xPrefix) {
        bHexIntegers += "0x";
    }
    if (size_8_16_or_32 == 8) {
        byte = HexMem_readByteContentFromIntReference(addr_ memAddress, 0);
        HexMem_appendHexByteToBuffer(bHexIntegers, byte);
        if (wantCharsToo) HexMem_appendSafeCharToBuffer(bAsciiCharacters, byte);
    } else if (size_8_16_or_32 == 16) {
        byte = HexMem_readByteContentFromIntReference(addr_ memAddress, 1);
        HexMem_appendHexByteToBuffer(bHexIntegers, byte);
        if (wantCharsToo) HexMem_appendSafeCharToBuffer(bAsciiCharacters, byte);
 
        byte = HexMem_readByteContentFromIntReference(addr_ memAddress, 0);
        HexMem_appendHexByteToBuffer(bHexIntegers, byte);
        if (wantCharsToo) HexMem_appendSafeCharToBuffer(bAsciiCharacters, byte);
    } else if (size_8_16_or_32 == 32) {
        byte = HexMem_readByteContentFromIntReference(addr_ memAddress, 3);
        HexMem_appendHexByteToBuffer(bHexIntegers, byte);
        if (wantCharsToo) HexMem_appendSafeCharToBuffer(bAsciiCharacters, byte);
 
        byte = HexMem_readByteContentFromIntReference(addr_ memAddress, 2);
        HexMem_appendHexByteToBuffer(bHexIntegers, byte);
        if (wantCharsToo) HexMem_appendSafeCharToBuffer(bAsciiCharacters, byte);
 
        byte = HexMem_readByteContentFromIntReference(addr_ memAddress, 1);
        HexMem_appendHexByteToBuffer(bHexIntegers, byte);
        if (wantCharsToo) HexMem_appendSafeCharToBuffer(bAsciiCharacters, byte);
 
        byte = HexMem_readByteContentFromIntReference(addr_ memAddress, 0);
        HexMem_appendHexByteToBuffer(bHexIntegers, byte);
        if (wantCharsToo) HexMem_appendSafeCharToBuffer(bAsciiCharacters, byte);
    } else {
        ack("HexMem_appendHexStringOfMemContent: Only BYTE (8), WORD (16), " //-
             "DWORD (32) sizes are supported");
        halt;
    }
}
 
//------------------------------------------------------------------------------
// Buffers must be pre-initialized
//------------------------------------------------------------------------------
void HexMem_appendHexStringOfInt(Buffer bHexInteger,
                                 Buffer bAsciiCharacters,
                                 int myInt,
                                 bool want0xPrefix) {
    int someInt = myInt;
    HexMem_appendHexStringOfMemContent(bHexInteger,
                                       bAsciiCharacters,             //-
                                       HexMem_addressOfVar(someInt), //-
                                       32,                           //-
                                       want0xPrefix);
}
 
//------------------------------------------------------------------------------
// Buffers must be pre-initialized
//------------------------------------------------------------------------------
void HexMem_appendHexStringOfInt(Buffer bHexInteger,
                                 Buffer bAsciiCharacters,
                                 int myInt,
                                 bool want0xPrefix) {
    int someInt = myInt;
    HexMem_appendHexStringOfMemContent(bHexInteger,
                                       bAsciiCharacters,             //-
                                       HexMem_addressOfVar(someInt), //-
                                       32,                           //-
                                       want0xPrefix);
}
 
//------------------------------------------------------------------------------
// Buffers must be pre-initialized
//------------------------------------------------------------------------------
void HexMem_appendHexStringOfInt(Buffer bHexInteger,
                                 int myInt,
                                 bool want0xPrefix) {
    int someInt = myInt;
    HexMem_appendHexStringOfMemContent(bHexInteger,
                                       (Buffer null()),              //-
                                       HexMem_addressOfVar(someInt), //-
                                       32,                           //-
                                       want0xPrefix);
}
 
string HexMem_hexStringOf(int myInt, bool want0xPrefix) {
    Buffer bHexIntegers = create(15);
    string retVal;
    HexMem_appendHexStringOfInt(bHexIntegers, myInt, want0xPrefix);
    retVal = stringOf(bHexIntegers);
    delete(bHexIntegers);
    return retVal;
}
 
 
//------------------------------------------------------------------------------
// Returns an int in its hex representation with 0x prefix
//------------------------------------------------------------------------------
string HexMem_hexStringOf(int myInt) {
    return HexMem_hexStringOf(myInt, true);
}
 
//------------------------------------------------------------------------------
// With a SkipString object, loads addresses into the key and content into the
// data.  You specify the start address and lengthInDwords.
//
// User options are:
// 1) use 0x prefix
// 2) add characters onto the end of the data (if printable)
//------------------------------------------------------------------------------
void HexMem_exploreAddressesIntoSkip(Skip skStrStr,
                                     int start,
                                     int lengthInDwords,
                                     bool use0xPrefix,
                                     bool addAsciiChars) {
    int i;
    int currentAddress = start;
 
    Buffer bHexAddress = create(15);
    Buffer bHexContent = create(25);
    Buffer bAsciiChars = null();
 
    if (addAsciiChars) {
        bAsciiChars = create(5);
    }
 
    for (i = 0; i < lengthInDwords; i++) {
        // The key contains the hex string of the address
        // The data contains the hex string of the content plus 4 chars if requested
        setempty(bHexAddress);
        HexMem_appendHexStringOfInt(bHexAddress, currentAddress, use0xPrefix);
 
        setempty(bHexContent);
        if (addAsciiChars) {
            setempty(bAsciiChars);
        }
        HexMem_appendHexStringOfMemContent(bHexContent,    //-
                                           bAsciiChars,    //
                                           currentAddress, //-
                                           32,             //-
                                           use0xPrefix);
        if (addAsciiChars) {
            put(skStrStr,                                                           //-
                (string stringOf(bHexAddress)),                                     //-
                (string stringOf(bHexContent) " ASCII: \"" stringOf(bAsciiChars)) "\"");
        } else {
            put(skStrStr,                                                           //-
                (string stringOf(bHexAddress)),                                     //-
                (string stringOf(bHexContent)) );
        }
        currentAddress += 4;
    }
    delete(bHexAddress);
    delete(bHexContent);
    if (addAsciiChars) {
        delete(bAsciiChars);
    }
}
 
//------------------------------------------------------------------------------
// With a pre-initialized SkipString object, loads addresses into the key and
// content into the data.  You specify the start address and lengthInDwords.
//
// Fixed Options are:
// 1) Will use 0x prefix
// 2) Will add characters onto the end of the data (if printable)
//------------------------------------------------------------------------------
void HexMem_exploreAddressesIntoSkip(Skip skStrStr,
                                     int start,
                                     int lengthInDwords) {
    bool use0xPrefix = true;
    bool addAsciiChars = true;
    HexMem_exploreAddressesIntoSkip(skStrStr,       //-
                                    start,          //-
                                    lengthInDwords, //-
                                    use0xPrefix,    //-
                                    addAsciiChars);
}
 
//------------------------------------------------------------------------------
// Sends 0xHexAddress = 0xHexContent CCCC to the  void outFunction(string) you
// specify as parameter 3.
//
// You specify the start address and lengthInDwords.  Fixed Options are:
// 1) Will use 0x prefix
// 2) Will add characters (C) onto the end of the data (if printable)
//------------------------------------------------------------------------------
void HexMem_exploreAddressesToFunction(int start,
                                       int lengthInDwords,
                                       void outFunction(string)) {
    Skip skStrStr = createString();
    HexMem_exploreAddressesIntoSkip(skStrStr, start, lengthInDwords);
    string memContent;
    for memContent in skStrStr do {
        outFunction("    " (string key(skStrStr)) " = " memContent "\n");
    }
    delete(skStrStr);
    skStrStr = null();
}
 
//------------------------------------------------------------------------------
// Prints 0xHexAddress = 0xHexContent CCCC
// You specify the start address and lengthInDwords.  Fixed Options are:
// 1) Will use 0x prefix
// 2) Will add characters (C) onto the end of the data (if printable)
//------------------------------------------------------------------------------
void HexMem_exploreAddressesToPrint(int start,
                                    int lengthInDwords) {
    void outFunction(string outData) {
        print(outData);
    }
    HexMem_exploreAddressesToFunction(start, lengthInDwords, outFunction);
}
 
//------------------------------------------------------------------------------
// END HexMem Utility
//------------------------------------------------------------------------------

 


SystemAdmin - Fri Nov 11 17:18:53 EST 2011

Re: Utility -- hexString, dumpMemory, DXL int fun facts.
SystemAdmin - Fri Nov 11 17:43:27 EST 2011

Whoops. Forgot to add the test code. See below:
 

// The posted source code ...
 
#include "HexMemUtils.dxl"
 
void debugLog(string str) {
    print(str "\n");
}
 
void test7() {
    int someInt;
 
    someInt = -2147483648;
    debugLog("Variable someInt was set to -2147483648 and now it is " //-
              HexMem_hexStringOf(someInt) " (" (someInt) ") decimal");
 
    someInt = -2147483647;
    debugLog("Variable someInt was set to -2147483647 and now it is " //-
              HexMem_hexStringOf(someInt) " (" (someInt) ") decimal");
 
    someInt = -2147483646;
    debugLog("Variable someInt was set to -2147483646 and now it is " //-
              HexMem_hexStringOf(someInt) " (" (someInt) ") decimal");
 
    someInt = 0x0FFFFFFF;
    debugLog("Variable someInt was set to 0x0FFFFFFF and now it is " //-
              HexMem_hexStringOf(someInt) " (" (someInt) ") decimal");
 
    someInt = 0x1FFFFFFF;
    debugLog("Variable someInt was set to 0x1FFFFFFF and now it is " //-
              HexMem_hexStringOf(someInt) " (" (someInt) ") decimal");
 
    someInt = 0x3FFFFFFF;
    debugLog("Variable someInt was set to 0x3FFFFFFF and now it is " //-
              HexMem_hexStringOf(someInt) " (" (someInt) ") decimal");
 
    someInt = 0x7FFFFFFF;
    debugLog("Variable someInt was set to 0x7FFFFFFF and now it is " //-
              HexMem_hexStringOf(someInt) " (" (someInt) ") decimal");
 
    someInt = 0xFFFFFFFF;
    debugLog("Variable someInt was set to 0xFFFFFFFF and now it is " //-
              HexMem_hexStringOf(someInt) " (" (someInt) ") decimal");
 
    debugLog("If you want to have control over the most significant byte," //-
             "you can do this (3 is the offset byte):");
    debugLog("HexMem_loadByteContentIntoIntReference(0xFF, someInt, 3);\n")
 
    HexMem_loadByteContentIntoIntReference(0xFF, someInt, 3);
    debugLog("Variable someInt upper byte changed to 0xFF and now it is " //-
              HexMem_hexStringOf(someInt) " (" (someInt) ") decimal\n");
 
    int anotherInt; // NOTE: unassigned
 
    int startAddress;
    startAddress= HexMem_addressOfVar(someInt);
 
    debugLog("Here is the assigned variable:");
    HexMem_exploreAddressesToPrint(startAddress, 1);
 
    startAddress = HexMem_addressOfVar(anotherInt);
    debugLog("Here is the unassigned variable:");
    HexMem_exploreAddressesToPrint(startAddress, 1);
 
    //debugLog("anotherInt = " (anotherInt) ""); // try uncommenting this
}
 
test7();

Re: Utility -- hexString, dumpMemory, DXL int fun facts.
Mathias Mamsch - Sat Nov 12 07:56:07 EST 2011

What you are saying is not entirely correct I think. DXL has full 32 Bit signed integers. You need to differentiate between two things in DXL

  • how DXL handles integer calculations
  • how DXL handles integer literals that are interpreted when parsing DXL


DXL does not do ceiling. It just calculates with signed 32 bit integers and will not report an integer overflow:

 

int a = 0x40000000
 
print a
print (a * 2) // Sign bit "overflow", prints  -2147483648 which is 0x80000000 as a signed integer
print (a * 4) // prints 0 which is correct, since the sign bit has been just pulled out of the 32 bit integer range
 
a = 0x7FFFFFFF  // largest integer, 31 bits set, sign bit 0 
 
print (a + 1)     // addition was performed correctly, no clipping occured



Regarding literals, you need to be aware, that when specifying binary octal or hexadecimal literals, you need to explitily set a sign, literals can only be 31 bit wide. A literal that is larger than 31 bit will indeed be set to MAXINT or -MAXINT depending on the sign (note that -MAXINT is not the smallest integer DOORS can handle)



 

int b = 0x7FFFFFFF
print b
b =  3000000000 // Yields MAXINT: 2147483647
print b 
b = -3000000000  // Yields -MAXINT: -2147483647
print b
 
b--;   // YIELDS MININT: -2147483648
print b

 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: Utility -- hexString, dumpMemory, DXL int fun facts.
Mathias Mamsch - Sat Nov 12 08:36:14 EST 2011

Mathias Mamsch - Sat Nov 12 07:56:07 EST 2011

What you are saying is not entirely correct I think. DXL has full 32 Bit signed integers. You need to differentiate between two things in DXL

  • how DXL handles integer calculations
  • how DXL handles integer literals that are interpreted when parsing DXL


DXL does not do ceiling. It just calculates with signed 32 bit integers and will not report an integer overflow:

 

int a = 0x40000000
 
print a
print (a * 2) // Sign bit "overflow", prints  -2147483648 which is 0x80000000 as a signed integer
print (a * 4) // prints 0 which is correct, since the sign bit has been just pulled out of the 32 bit integer range
 
a = 0x7FFFFFFF  // largest integer, 31 bits set, sign bit 0 
 
print (a + 1)     // addition was performed correctly, no clipping occured



Regarding literals, you need to be aware, that when specifying binary octal or hexadecimal literals, you need to explitily set a sign, literals can only be 31 bit wide. A literal that is larger than 31 bit will indeed be set to MAXINT or -MAXINT depending on the sign (note that -MAXINT is not the smallest integer DOORS can handle)



 

int b = 0x7FFFFFFF
print b
b =  3000000000 // Yields MAXINT: 2147483647
print b 
b = -3000000000  // Yields -MAXINT: -2147483647
print b
 
b--;   // YIELDS MININT: -2147483648
print b

 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Forgot to post the part about specifying real 32 Bit literals. For this one needs to know, how to convert a binary to its negative value. It is pretty easy to remember when you think about 0 = -0, i.e. 0 should keep its value when inverting. To invert a binary number you will just flip all the bits and add 1 (try this with the windows calculator, not with DXL):
 

// FLIPPED HEXES:
// F E D C B A 9 8 7 6 5 4 3 2 1 0
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
 
 3  =  0x00000003
-3  =  0xFFFFFFFC + 1 = 0xFFFFFFFD

 

But how can I now specify -3 (or any other 32 Bit hex number) as a HEX literal? I need to clip the sign bit by putting a minus in front of it, and then do the inversion myself.

int signBit = 0x7FFFFFFF + 1 // get the 0x80000000 by addition
int minus1 = signBit + 0x7FFFFFFF // make 0xFFFFFFFF = -1
 
// To make an arbitrary 32 Bit int literal like 0xA0A0A0A0 I need do find the
// negative number that corresponds to my number:
// 0xA0A0A0A0 = - (-0xA0A0A0A0) = -(0x5F5F5F5F + 1) = -0x5F5F5F60
 
int bitPattern = -0x5F5F5F60 
print bitPattern 
 
bitPattern = 0x20A0A0A0 + signBit 
print bitPattern



Regards, Mathias



 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS